package cn.iocoder.yudao.module.forward.mqtt;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.forward.dal.dataobject.broker.BrokerDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasource.DataSourceDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasourceinfo.DataSourceInfoDO;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
//import cn.iocoder.yudao.module.forward.dal.dataobject.datasouce.DataSouceDO;


@Slf4j
public class MqttInfoCreateUtil {
    public static MqttInfo createMqttInfoByDataSource(DataSourceDO dataSourceDO, DataSourceInfoDO info) {
        if (info == null) {
            return null;
        }
        MqttInfo mqttInfo = new MqttInfo();
        BeanUtils.copyProperties(info, mqttInfo);
        mqttInfo.setName(dataSourceDO.getName());
        mqttInfo.setId(dataSourceDO.getId());
        mqttInfo.setKeepAliveInterval(MqttConstants.KEEPALIVE_FORWARD);
//        List<String> topics = new ArrayList<>();
//        if (!StringUtils.isEmpty(info.getTopicEew())) {
//            topics.add(info.getTopicEew());
//        }
//        if (!StringUtils.isEmpty(info.getTopicEqr())) {
//            topics.add(info.getTopicEqr());
//        }
//        mqttInfo.setTopics(topics);
        return mqttInfo;
    }

    public static MqttInfo createMqttInfoByBroker(BrokerDO info) {
        if (info == null) {
            return null;
        }
        MqttInfo mqttInfo = new MqttInfo();
        BeanUtils.copyProperties(info, mqttInfo);
        mqttInfo.setName(info.getClientName());
        mqttInfo.setKeepAliveInterval(MqttConstants.KEEPALIVE_BROKER);
        return mqttInfo;
    }

    public static void tryDataSourceConnect(MqttManager mqttManager, DataSourceDO dataSourceDO, DataSourceInfoDO dataSourceInfoDO) {
        if (!dataSourceDO.getEnable()) {
            log.info("{}({})未开启，停止连接", mqttManager.getTag(), dataSourceDO.getName());
            return;
        }

        boolean ret = validateDataSourceInfoDO(dataSourceInfoDO);
        if (!ret) {
            log.info("{}({})数据缺失，停止连接", mqttManager.getTag(), dataSourceDO.getName());
            return;
        }

        MqttConnect mqttConnect = mqttManager.getMqttConnect(dataSourceDO.getName());
        MqttInfo mqttInfo = null;
        if (mqttConnect != null) {
            mqttInfo = mqttConnect.getMqttInfo();
            if (!checkEquals(mqttInfo, dataSourceInfoDO)) {
                log.info("{}({})数据未发生改变，不重新连接", mqttManager.getTag(), dataSourceDO.getName());
                return;
            }
            mqttManager.closeMqttConnect(dataSourceDO.getName());
        }
        mqttInfo = MqttInfoCreateUtil.createMqttInfoByDataSource(dataSourceDO, dataSourceInfoDO);
        mqttManager.doConnect(mqttInfo);
    }

    public static void tryBrokerConnect(MqttManager mqttManager, BrokerDO brokerDO) {
        if (!brokerDO.getEnable()) {
            log.info("{}({})未开启，停止连接", mqttManager.getTag(), brokerDO.getClientName());
            return;
        }

        boolean ret = validateBrokerDO(brokerDO);
        if (!ret) {
            log.info("{}({})数据缺失，停止连接", mqttManager.getTag(), brokerDO.getClientName());
            return;
        }

        MqttConnect mqttConnect = mqttManager.getMqttConnect(brokerDO.getClientName());
        MqttInfo mqttInfo = null;
        if (mqttConnect != null) {
            mqttInfo = mqttConnect.getMqttInfo();
            if (!checkEquals(mqttInfo, brokerDO)) {
                log.info("{}({})数据未发生改变，不重新连接", mqttManager.getTag(), brokerDO.getClientName());
                return;
            }
            mqttManager.closeMqttConnect(brokerDO.getClientName());
        }
        mqttInfo = MqttInfoCreateUtil.createMqttInfoByBroker(brokerDO);
        mqttManager.doConnect(mqttInfo);
    }

    public static boolean validateDataSourceInfoDO(DataSourceInfoDO info) {
        return info != null &&
                !StringUtils.isEmpty(info.getClientId()) &&
                !StringUtils.isEmpty(info.getUsername()) &&
                !StringUtils.isEmpty(info.getPwd()) &&
                !StringUtils.isEmpty(info.getIp()) &&
                info.getPort() != null;
    }

    public static boolean validateBrokerDO(BrokerDO info) {
        return info != null &&
                !StringUtils.isEmpty(info.getClientId()) &&
                !StringUtils.isEmpty(info.getUsername()) &&
                !StringUtils.isEmpty(info.getPwd()) &&
                !StringUtils.isEmpty(info.getIp()) &&
                info.getPort() != null;
    }

    public static boolean checkEquals(MqttInfo mqttInfo, DataSourceInfoDO dataSourceInfoDO) {
        return mqttInfo.getClientId().equals(dataSourceInfoDO.getClientId()) &&
                mqttInfo.getIp().equals(dataSourceInfoDO.getIp()) &&
                mqttInfo.getPwd().equals(dataSourceInfoDO.getPwd()) &&
                mqttInfo.getUsername().equals(dataSourceInfoDO.getUsername()) &&
                mqttInfo.getPort() == dataSourceInfoDO.getPort();
    }

    public static boolean checkEquals(MqttInfo mqttInfo, BrokerDO brokerDO) {
        return mqttInfo.getClientId().equals(brokerDO.getClientId()) &&
                mqttInfo.getIp().equals(brokerDO.getIp()) &&
                mqttInfo.getPwd().equals(brokerDO.getPwd()) &&
                mqttInfo.getUsername().equals(brokerDO.getUsername()) &&
                mqttInfo.getPort() == brokerDO.getPort();
    }
}
